<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      width="80%"
      align-center
      :show-close="false"
      draggable
      @close="handleClose"
    >
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>意见汇总</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close"
            ><CircleCloseFilled color="#fff"
          /></el-icon>
        </div>
      </template>
      <div class="bg-white">
        <div class="bg-white my-[10px]">
          <BaseHead style="border-bottom: 0">评分详情</BaseHead>
          <BaseTable>
            <tr>
              <td class="td-name">项目名称</td>
              <td style="width: 87.5%" colspan="3">{{ detailData.adminOptionsDto?.bookName }}</td>
            </tr>
          </BaseTable>
          <el-table :data="remakeList" class="w-full" border stripe show-summary>
            <el-table-column label="标准">
              <el-table-column prop="joinStandardName" label="标准名称" />
              <el-table-column prop="joinStandardFullMark" label="满分" />
            </el-table-column>
            <el-table-column label="专家评分详情" align="center">
              <el-table-column
                :label="item.reviewerName"
                :prop="item.reviewerName"
                v-for="item in detailData.remakeDetail"
              />
              <el-table-column prop="平均分" label="平均分" />
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="bg-white mt-[10px]">
        <BaseHead style="border-bottom: 0">专家意见</BaseHead>
        <el-table :data="detailData.expertOptionsList" class="w-full" border>
          <el-table-column prop="reviewerName" label="专家姓名" />
          <el-table-column prop="reviewerInfo" label="专家意见" />
          <el-table-column label="评审结果">
            <template #default="{ row }">
              <span v-if="row.expertStatus == 'refuseReview'">拒评</span>
              <CustomLabel
                :map="{ pass: '建议通过', noPass: '不建议通过' }"
                :mapKey="row.reviewerResult"
                v-else
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="bg-white mt-[10px] py-[10px]">
        <el-form :model="editForm" ref="editFormRef">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="" label-width="0" prop="reviewInfo">
                <el-input
                  placeholder="评审意见"
                  clearable
                  v-model="editForm.reviewInfo"
                  type="textarea"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="最终评审结果：" prop="reviewResult">
                <el-radio-group v-model="editForm.reviewResult">
                  <el-radio value="pass">建议通过</el-radio>
                  <el-radio value="noPass">不建议通过</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="save">保存</el-button>
            <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import BaseTable from "@/views/project/components/BaseTable.vue";
import CustomLabel from "@/views/project/components/CustomLabel.vue";
import { getSuggestDetail, editSuggest } from "@/api/project/xj/xmps/psxm";
import { ElMessage } from "element-plus";
import { assignObj } from "@/utils/main";
const dialogVisible = ref(false);
const bookId = ref(null);
const detailData = ref({});
const remakeList = ref([]); //评分数据列表
const editFormRef = ref({});
const editForm = reactive({
  reviewInfo: "",
  reviewResult: "pass",
});
//获取数据详情
const getDetail = async () => {
  const params = {
    bookId: bookId.value,
  };
  try {
    const res = await getSuggestDetail(params);
    detailData.value = res.data;
    nextTick(() => {
      assignObj(editForm, detailData.value.adminOptionsDto);
    });
    const remakeDetail = res.data.remakeDetail;
    if (remakeDetail.length) {
      //获取标准名称数组
      const standardNames = remakeDetail[0].joinStandardName
        ? remakeDetail[0].joinStandardName.split(",")
        : [];
      //获取标准总分数组
      const standardFullMarks = remakeDetail[0].joinStandardFullMark
        ? remakeDetail[0].joinStandardFullMark.split(",")
        : [];
      //数据处理成 {joinStandardName: "项目先进性",joinStandardFullMark: "100",张三: 10,李四: 6,平均分: 8,}这种格式
      standardNames.forEach((item, index) => {
        const remakeItem = {
          joinStandardName: item,
          joinStandardFullMark: standardFullMarks[index],
        };
        let total = 0;
        let personNumber = 0; //非拒绝评审的人数
        remakeDetail.forEach((pItem) => {
          const score = pItem.joinStandardRealMark.split(",")[index] || "";
          remakeItem[pItem.reviewerName] = score;
          total += score != "" && score != "-" ? parseFloat(score) : 0;
          if (pItem.expertStatus != "refuseReview") {
            personNumber += 1;
          }
        });
        //计算平均分
        remakeItem["平均分"] = (total / personNumber).toFixed(1);
        remakeList.value.push(remakeItem);
      });
      console.log(remakeList.value);
    }
  } catch (error) {
    console.log(error);
  }
};
// 编辑
const edit = (id) => {
  bookId.value = id;
  dialogVisible.value = true;
  getDetail();
};
defineExpose({ edit });
const emit = defineEmits(["save-success"]);
//点击保存
const save = async () => {
  try {
    let params = {
      bookId: bookId.value,
      ...editForm,
    };
    const res = await editSuggest(params);
    if (res.code === 200) {
      ElMessage({
        message: "操作成功！",
        type: "success",
      });
      emit("save-success");
      dialogVisible.value = false;
    }
  } catch (error) {
    console.log(error);
  }
};
const handleClose = () => {
  editFormRef.value.resetFields();
  remakeList.value = [];
};
</script>

<style scoped lang="scss">
@import url("@/views/project/index.scss");
</style>
